//
//  NSDate+NYExtension.m
//  百思不得姐
//
//  Created by Apple on 15/8/24.
//  Copyright (c) 2015年 Neal. All rights reserved.
//

#import "NSDate+NYExtension.h"

@implementation NSDate (NYExtension)

static NSDateFormatter *formatter_;
+ (void)load
{
    formatter_ = [[NSDateFormatter alloc]init];
}
- (BOOL)isThisYear
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSInteger nowYear = [calendar component:NSCalendarUnitYear fromDate:[NSDate date]];
    NSInteger selfYear = [calendar component:NSCalendarUnitYear fromDate:self];
    
    return nowYear == selfYear;
}
- (BOOL)isToday
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSCalendarUnit unit = NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay;
    NSDateComponents *nowComps = [calendar components:unit fromDate:[NSDate date]];
    NSDateComponents *selfComps = [calendar components:unit fromDate:self];
    
    return nowComps.year == selfComps.year && nowComps.month == selfComps.month && nowComps.day == selfComps.day;
}
- (BOOL)isYesterday
{
    formatter_.dateFormat = @"yyyy-MM-dd";
    
    NSString *nowString = [formatter_ stringFromDate:[NSDate date]];
    NSDate *nowDate = [formatter_  dateFromString:nowString];
    
    NSString *selfString = [formatter_ stringFromDate:self];
    NSDate *selfDate = [formatter_ dateFromString:selfString];
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSCalendarUnit unit = NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay;
    NSDateComponents *nowComps = [calendar components:unit fromDate:selfDate toDate:nowDate options:0];

    return nowComps.year == 0 && nowComps.month ==0 && nowComps.day == 1;
}
- (BOOL)isTomorrow
{
    formatter_.dateFormat = @"yyyy-MM-dd";
    
    NSString *nowString = [formatter_ stringFromDate:[NSDate date]];
    NSDate *nowDate = [formatter_  dateFromString:nowString];
    
    NSString *selfString = [formatter_ stringFromDate:self];
    NSDate *selfDate = [formatter_ dateFromString:selfString];
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSCalendarUnit unit = NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay;
    NSDateComponents *nowComps = [calendar components:unit fromDate:nowDate toDate:selfDate options:0];
    
    return nowComps.year == 0 && nowComps.month ==0 && nowComps.day == -1;
}
- (NSDateComponents *)intervalToNow
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    
    NSDateComponents *comps = [calendar components:unit fromDate:self toDate:[NSDate date] options:0];
    
    return comps;
}
@end
